package cn.bdqn.config;

import cn.bdqn.entity.Dog;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.beans.PropertyVetoException;

@Configuration
//扫描包，
@ComponentScan(basePackages = {"cn.bdqn.controller","cn.bdqn.service","cn.bdqn.config"})
//指定属性配置文件
@PropertySource(value = "classpath:db.properties")
//开启MVC的注解驱动，等价于<mvc:annotation-driven/>
@EnableWebMvc
//扫描DAO层接口
@MapperScan(basePackages = "cn.bdqn.dao")
public class SpringConfig {
    @Value("${driver}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${uname}")
    private String uname;
    @Value("${pwd}")

    private String pwd;
    @Bean("datasource")
    public ComboPooledDataSource getDataSource() {
        ComboPooledDataSource source = new ComboPooledDataSource();
        try {
            source.setDriverClass(driver);
            source.setJdbcUrl(url);
            source.setUser(uname);
            source.setPassword(pwd);
        } catch (PropertyVetoException e) {
            throw new RuntimeException("属性配置文件错误！");
        }
        return source;
    }

}
